package com.maoslv.controller;

import com.maoslv.aop.LogAnnotation;
import com.maoslv.dto.Result;
import com.maoslv.pojo.Pagination;
import com.maoslv.pojo.Staff;
import com.maoslv.service.staff.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/staff")
@Scope("request")
public class StaffController {
    @Autowired
    StaffService staffService;

    @PostMapping("/staff_list")
    @LogAnnotation(operator = "获取员工列表")
    @PreAuthorize("hasAnyAuthority('staff:list')")
    public Result getStaffList(@RequestBody Pagination pagination) {
        Map<Object,Object> result = staffService.getStaffList(pagination);
        if (result != null) {
            return Result.succ("获取成功", result);
        }
        return Result.fail("获取失败");
    }

    @GetMapping("/department_list")
    @PreAuthorize("hasAnyAuthority('staff:list')")
    @LogAnnotation(operator = "获取部门列表")
    public Result getStaffList() {
        List<String> departmentList = staffService.getDepartmentList();
        if (departmentList != null) {
            return Result.succ("获取成功", departmentList);
        }
        return Result.fail("获取失败");
    }

    @GetMapping("/SatffInfo/{id}")
    @LogAnnotation(operator = "通过ID获取员工信息")
    @PreAuthorize("hasAnyAuthority('staff:update')")
    public Result getSatffInfo(@PathVariable Integer id) {
        Staff staff = staffService.getStaffInfo(id);
        if (staff != null) {
            return Result.succ("获取成功", staff);
        }
        return Result.fail("获取失败");
    }

    @PostMapping("/updateStaff")
    @LogAnnotation(operator = "编辑员工")
    public Result updateStaff(@RequestBody Staff staff) {
        if (staffService.updateStaff(staff) > 0) {
            return Result.succ("编辑成功", null);
        }
        return Result.fail("编辑失败");
    }

    @PostMapping("/addStaff")
    @LogAnnotation(operator = "新增员工")
    @PreAuthorize("hasAnyAuthority('staff:add')")
    public Result addStaff(@RequestBody Staff staff) {
        if (staffService.addStaff(staff) > 0) {
            return Result.succ("新增成功", null);
        }
        return Result.fail("新增失败");
    }
}
